package com.fanfan.SmartPensionSystembackend.controller;

import com.fanfan.SmartPensionSystembackend.entity.Activity;
import com.fanfan.SmartPensionSystembackend.service.ActivityService;
import com.fanfan.SmartPensionSystembackend.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Description ActivityController
 * @Author Guangdi Chen
 * @Date 2025-03-23
 */
@RestController
@RequestMapping("/activity")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    // 添加活动
    @PostMapping("/addActivity")
    public Map<String, Object> addActivity(@RequestBody Activity activity) {
        try {
            System.out.println(activity);
            boolean result = activityService.addActivity(activity);
            if (result) {
                return ResponseUtil.success("添加成功", null);
            } else {
                return ResponseUtil.error("添加失败：数据保存异常");
            }
        } catch (Exception e) {
            return ResponseUtil.error("添加失败：" + e.getMessage());
        }
    }

    // 获取活动列表
    @GetMapping("/getAllActivity")
    public Map<String, Object> getAllActivity() {
        try {
            List<Activity> activityList = activityService.getAllActivity();
            return ResponseUtil.success("查询成功", activityList);
            // 获取所有用户信息
       }catch (Exception e)
            {
                return ResponseUtil.error("查询失败：" + e.getMessage());
            }
    }

    // 通过审核
    @PostMapping("/passStatus")
    public Map<String, Object> updateStatus(@RequestBody Map<String, String> params) {
        try {
            String activityId = params.get("activityId");
            String status = params.get("status");
            boolean result = activityService.updateStatus(activityId, status);
            if (result) {
                return ResponseUtil.success("更新成功", null);
            } else {
                return ResponseUtil.error("更新失败：数据保存异常");
            }
        } catch (Exception e) {
            return ResponseUtil.error("更新失败：" + e.getMessage());
        }
    }

    // 驳回审核
    @PostMapping("/rejectStatus")
    public Map<String, Object> rejectStatus(@RequestBody Map<String, String> params) {
        try {
            String activityId = params.get("activityId");
            String status = params.get("status");
            String rejectReason = params.get("rejectReason");
            boolean result = activityService.rejectStatus(activityId, status, rejectReason);
            if (result) {
                return ResponseUtil.success("更新成功", null);
            } else {
                return ResponseUtil.error("更新失败：数据保存异常");
            }
        }catch (Exception e){
            return ResponseUtil.error("更新失败：" + e.getMessage());
        }
    }

    // 根据状态筛选活动
    @GetMapping("/getActivityListByStatus")
    public Map<String, Object> getActivityListByStatus(@RequestParam String status) {
        try {
            List<Activity> activityList = activityService.getActivityListByStatus(status);
            return ResponseUtil.success("查询成功", activityList);
        } catch (Exception e) {
            return ResponseUtil.error("查询失败：" + e.getMessage());
        }
    }
}
